const Category = require('../model/category');
/**
 * 文章中间件
 */
module.exports = {
    /**
     * 获取文章类目列表
     */
    getList:(req,res,next) =>{
        Category.getList().then(results =>{
            req.category = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
     /**
     * 获取指定编号的类目列表
     */
    getCategoryId:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Category.getCategoryId(id).then(results =>{
            req.categoryId = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 添加类目
     */
    add:(req,res,next) =>{
        let {name,index} = req.body;
        Category.add(name,index).then(results =>{
            req.insertId = results;
            console.log(results)
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     *删除类目
     */
    del:(req,res,next) =>{
        let {id} = req.query;
        Category.del(id).then(results =>{
            req.affectedRows = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 修改类目名称
    */
    setName:(req,res,next) =>{
        let {id,name} = req.body;
        Category.setName(id,name).then(results =>{
            req.affectedRows = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 修改类目的排序
    */
    setIndex:(req,res,next) =>{
        let {id,index} = req.body;
        Category.setIndex(id,index).then(results =>{
            req.affectedRows = results;
            next();
        }).catch(err=>{
            next(err);
        });
    }
}